import axios from 'axios';
// Actions
const DUBBO_FETCH_CASE_COL_LIST = 'yapi/dubboCase/FETCH_CASE_COL_LIST';
const DUBBO_FETCH_CASE_DATA = 'yapi/dubboCase/FETCH_CASE_DATA';
const DUBBO_FETCH_CASE_LIST = 'yapi/dubboCase/FETCH_CASE_LIST';
const DUBBO_SET_COL_DATA = 'yapi/dubboCase/SET_COL_DATA';
const DUBBO_FETCH_VARIABLE_PARAMS_LIST = 'yapi/dubboCase/FETCH_VARIABLE_PARAMS_LIST';
const DUBBO_FETCH_CASE_ENV_LIST = 'yapi/dubboCase/FETCH_CASE_ENV_LIST';
// Reducer
const initialState = {
    caseColList: [
        {
            _id: 0,
            name: '',
            uid: 0,
            projectid: 0,
            desc: '',
            add_time: 0,
            up_time: 0,
            caseList: [{}]
        }
    ],
    isShowCol: true,
    isRender: false,
    currColId: 0,
    currCaseId: 0,
    currCase: {},
    currCaseList: [],
    variableParamsList: [],
    envList: []
};

export default (state = initialState, action) => {
    switch (action.type) {
        case DUBBO_FETCH_CASE_COL_LIST: {
            return {
                ...state,
                caseColList: action.payload.data.data
            };
        }
        case DUBBO_FETCH_CASE_DATA: {
            return {
                ...state,
                currCase: action.payload.data.data
            };
        }
        case DUBBO_FETCH_CASE_LIST: {
            return {
                ...state,
                currCaseList: action.payload.data.data
            };
        }

        case DUBBO_FETCH_VARIABLE_PARAMS_LIST: {
            return {
                ...state,
                variableParamsList: action.payload.data.data
            };
        }
        case DUBBO_SET_COL_DATA: {
            return {
                ...state,
                ...action.payload
            };
        }
        case DUBBO_FETCH_CASE_ENV_LIST: {
            return {
                ...state,
                envList: action.payload.data.data
            };
        }
        default:
            return state;
    }
};

// Action Creators
export function fetchCaseColList(dubboId) {
    return {
        type: DUBBO_FETCH_CASE_COL_LIST,
        payload: axios.get('/api/case_col/list?projectid=' + dubboId)
    };
}

export function fetchCaseData(caseId) {
    return {
        type: DUBBO_FETCH_CASE_DATA,
        payload: axios.get('/api/case_col/case?caseid=' + caseId)
    };
}

export function fetchCaseList(colId) {
    return {
        type: DUBBO_FETCH_CASE_LIST,
        payload: axios.get('/api/case_col/case_list/?col_id=' + colId)
    };
}

export function fetchCaseEnvList(col_id) {
    return {
        type: DUBBO_FETCH_CASE_ENV_LIST,
        payload: axios.get('/api/case_col/case_env_list', {
            params: { col_id }
        })
    };
}

export function fetchVariableParamsList(colId) {
    return {
        type: DUBBO_FETCH_VARIABLE_PARAMS_LIST,
        payload: axios.get('/api/case_col/case_list_by_var_params?col_id=' + colId)
    };
}

export function setColData(data) {
    return {
        type: DUBBO_SET_COL_DATA,
        payload: data
    };
}
